package com.hzncc.flowable_diboot.service.impl;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.util.S;
import com.diboot.core.vo.JsonResult;
import com.diboot.iam.util.IamSecurityUtils;
import com.diboot.notification.config.Cons;
import com.diboot.notification.entity.Message;
import com.hzncc.flowable_diboot.entity.Customer;
import com.hzncc.flowable_diboot.entity.SalesTeam;
import com.hzncc.flowable_diboot.mapper.CustomerMapper;
import com.hzncc.flowable_diboot.mapper.SalesTeamMapper;
import com.hzncc.flowable_diboot.rabbitmq.service.impl.SendMessageService;
import com.hzncc.flowable_diboot.service.SalesTeamService;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import com.hzncc.flowable_diboot.vo.SalesTeamListVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


/**
* 销售团队 相关Service实现类
* @author MyName
* @version 1.0
* @date 2024-05-21
* Copyright © MyCorp
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class SalesTeamServiceImpl extends BaseServiceImpl<SalesTeamMapper, SalesTeam> implements SalesTeamService {

    private final SendMessageService sendMessageService;
    private final CustomerMapper customerMapper;
    @Override
    public JsonResult<?> kickTheTeam(String id) {
        SalesTeamListVO entity = getViewObject(id, SalesTeamListVO.class);
        String customerId = entity.getCustomerId();
        Customer customer = customerMapper.selectById(customerId);
        if (!S.equals(customer.getPrincipal(),IamSecurityUtils.getCurrentUserId())) {
            return JsonResult.FAIL_NO_PERMISSION("权限不足，请联系团队负责人");
        }
        deleteEntity(id);
        Message msg = new Message();
        msg.setReceiver(entity.getMember());
        msg.setTitle("客户团队变动");
        msg.setContent("您已被移出【" + entity.getCustomerIdLabel() + "】团队");
        msg.setAppModule(ConstantUtil.MODEL_SALES);
        msg.setSender(IamSecurityUtils.getCurrentUserId());
        msg.setStatus(Cons.MESSAGE_STATUS.DELIVERY.name());
        msg.setChannel(Cons.MESSAGE_CHANNEL.SYS_MSG.name());
        msg.setBusinessType("");
        msg.setTemplateId("");
        msg.setBusinessCode("");
        sendMessageService.sendRetainedMessage(msg);
        return JsonResult.OK();
    }
}